package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.entity.*;
import com.hmdp.mapper.TicketMapper;
import com.hmdp.mapper.TicketOrderMapper;
import com.hmdp.mapper.UserMapper;
import com.hmdp.utils.RedisIdWorker;
import com.hmdp.utils.UserHolder;
import netscape.javascript.JSObject;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

@Service
public class SecServiceImpl {
    //    @Resource
//    ISecService iSecService;
    @Resource
    TicketOrderMapper ticketOrderMapper;

    @Resource
    TicketMapper ticketMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    public Result ticketSeckill(Long eventId) {
        String eventCache = stringRedisTemplate.opsForValue().get(eventId.toString());

        // 如果缓存为空，则查询数据库，数据库更新后，并写在缓存上
        if (StrUtil.isBlank(eventCache)) {
            // 1.查询赛事

            Ticket ticket = ticketMapper.selectById(eventId);
        // 2.判断秒杀是否开始
            if (ticket.getEventDate().isAfter(LocalDateTime.now())) {
            // 尚未开始
                return Result.fail("秒杀尚未开始！");
            }
        // 3.判断秒杀是否已经结束
            if (ticket.getDeadLineTime().isBefore(LocalDateTime.now())) {
////            // 尚未开始
                return Result.fail("秒杀已经结束！");
            }
//        // 4.判断库存是否充足
            if (ticket.getTicketStock() < 1) {
                // 库存不足
                return Result.fail("库存不足！");
            }

            // 6.扣减库存
            int count = ticketMapper.reduceTicket(eventId);
            if (count < 1) {
                return Result.fail("库存不足");
            }


            // 7.创建订单

//                // 7.1.订单id
            TicketOrder ticketOrder = new TicketOrder(new Random().nextInt(), 1, LocalDateTime.now(), null, null);

            ticketOrderMapper.insert(ticketOrder);

            ticket.setTicketStock(ticket.getTicketStock() - 1);
            stringRedisTemplate.opsForValue().set(eventId.toString(), JSONUtil.toJsonStr(ticket));


            return Result.ok(eventId);
        } else {

            //如果缓存有，数据库的扣减库存后，更新缓存



////        // 1.查询赛事
            Ticket ticketCache = JSONUtil.toBean(eventCache, Ticket.class);

////        // 2.判断秒杀是否开始
            if (ticketCache.getEventDate().isAfter(LocalDateTime.now())) {
////            // 尚未开始
                return Result.fail("秒杀尚未开始！");
            }
//        // 3.判断秒杀是否已经结束
            if (ticketCache.getDeadLineTime().isBefore(LocalDateTime.now())) {
////            // 尚未开始
                return Result.fail("秒杀已经结束！");
            }
//        // 4.判断库存是否充足
            if (ticketCache.getTicketStock() < 1) {
                // 库存不足
                return Result.fail("库存不足！");
            }

            // 6.扣减库存
            int count = ticketMapper.reduceTicket(eventId);
            if (count < 1) {
                return Result.fail("库存不足");
            }


            // 7.创建订单

//                // 7.1.订单id
            TicketOrder ticketOrder = new TicketOrder(new Random().nextInt(), 1, LocalDateTime.now(), null, null);

            ticketOrderMapper.insert(ticketOrder);

            ticketCache.setTicketStock(ticketCache.getTicketStock() - 1);
            stringRedisTemplate.opsForValue().set(eventId.toString(), JSONUtil.toJsonStr(ticketCache));


            return Result.ok(eventId);
        }
    }
}




